package com.spic.business.riskbase.hierarchy.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.spic.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.spic.business.riskbase.hierarchy.domain.RiskDescription;
import com.spic.business.riskbase.hierarchy.mapper.RiskDangerSourceMapper;
import com.spic.business.riskbase.hierarchy.domain.RiskDangerSource;
import com.spic.business.riskbase.hierarchy.service.IRiskDangerSourceService;

/**
 * dangerSourceService业务层处理
 *
 * @author ruoyi
 * @date 2023-04-14
 */
@Service
public class RiskDangerSourceServiceImpl implements IRiskDangerSourceService
{
    @Autowired
    private RiskDangerSourceMapper riskDangerSourceMapper;

    /**
     * 查询dangerSource
     *
     * @param id dangerSource主键
     * @return dangerSource
     */
    @Override
    public RiskDangerSource selectRiskDangerSourceById(Long id)
    {
        return riskDangerSourceMapper.selectRiskDangerSourceById(id);
    }

    /**
     * 查询dangerSource列表
     *
     * @param riskDangerSource dangerSource
     * @return dangerSource
     */
    @Override
    public List<RiskDangerSource> selectRiskDangerSourceList(RiskDangerSource riskDangerSource)
    {
        return riskDangerSourceMapper.selectRiskDangerSourceList(riskDangerSource);
    }

    /**
     * 新增dangerSource
     *
     * @param riskDangerSource dangerSource
     * @return 结果
     */
    @Transactional
    @Override
    public int insertRiskDangerSource(RiskDangerSource riskDangerSource)
    {
        int rows = riskDangerSourceMapper.insertRiskDangerSource(riskDangerSource);
        insertRiskDescription(riskDangerSource);
        return rows;
    }

    /**
     * 修改dangerSource
     *
     * @param riskDangerSource dangerSource
     * @return 结果
     */
    @Transactional
    @Override
    public int updateRiskDangerSource(RiskDangerSource riskDangerSource)
    {
        riskDangerSourceMapper.deleteRiskDescriptionByDangerSourceId(riskDangerSource.getId());
        insertRiskDescription(riskDangerSource);
        return riskDangerSourceMapper.updateRiskDangerSource(riskDangerSource);
    }

    /**
     * 批量删除dangerSource
     *
     * @param ids 需要删除的dangerSource主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRiskDangerSourceByIds(Long[] ids)
    {
        riskDangerSourceMapper.deleteRiskDescriptionByDangerSourceIds(ids);
        return riskDangerSourceMapper.deleteRiskDangerSourceByIds(ids);
    }

    /**
     * 删除dangerSource信息
     *
     * @param id dangerSource主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRiskDangerSourceById(Long id)
    {
        riskDangerSourceMapper.deleteRiskDescriptionByDangerSourceId(id);
        return riskDangerSourceMapper.deleteRiskDangerSourceById(id);
    }

    @Override
    public int deleteDangerSourceByWorkStepId(Long id) {
        return riskDangerSourceMapper.deleteDangerSourceByWorkStepId(id);
    }

    @Override
    public int batchRiskDangerSource(List<RiskDangerSource> list) {
        return riskDangerSourceMapper.batchRiskDangerSource(list);
    }

    /**
     * 新增${subTable.functionName}信息
     *
     * @param riskDangerSource dangerSource对象
     */
    public void insertRiskDescription(RiskDangerSource riskDangerSource)
    {
        List<RiskDescription> riskDescriptionList = riskDangerSource.getRiskDescriptionList();
        Long id = riskDangerSource.getId();
        if (StringUtils.isNotNull(riskDescriptionList))
        {
            List<RiskDescription> list = new ArrayList<RiskDescription>();
            for (RiskDescription riskDescription : riskDescriptionList)
            {
                riskDescription.setDangerSourceId(id);
                list.add(riskDescription);
            }
            if (list.size() > 0)
            {
                riskDangerSourceMapper.batchRiskDescription(list);
            }
        }
    }
}
